package com.laiat.location.service;


import java.util.Timer;

import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.IBinder;
import android.util.Log;

public class SendLocationService extends IntentService {

	private static final String TAG = SendLocationService.class.getSimpleName();
	
	private static final long LOCATION_MIN_TIME = 3600000;  // one hour
	private static final float LOCATION_MIN_DISTANCE = 1000; // one kilometer

	public SendLocationService() {
		super(TAG);
		
		Log.d(TAG, "SendLocationService constructed");
		
	}
	
	@Override
	protected void onHandleIntent(Intent inIntent) {
		Log.d(TAG, "onHandleIntent'ing");

		//get location
		Location location = getLocation();
		
		if (location != null) {
		
			//send email if data (mobile or wifi) connectivity is okay
			
			//save to db
			
			//update listview activity
		}
		
		//unregister

	}
	
	private Location getLocation() {
		Location location = null;
		
		// setup location info
		LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
		
		if (locationManager != null) {
			//first try gps provider
			location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			if (location == null) {
				location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
			}

			if (location == null) {
				//wait for 5 minutes
				
				Timer timer = new Timer();
				
				
			}
			
			
		//	locationManager.requestLocationUpdates(provider, LOCATION_MIN_TIME, LOCATION_MIN_DISTANCE, this);
		}
		
		return null;
	}

	
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
